package com.directv.z8training.EMS.Controller;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.directv.z8training.EMS.Entity.ProjectUser;
import com.directv.z8training.EMS.Entity.User;
import com.directv.z8training.EMS.Services.ProjectManagement;
import com.directv.z8training.EMS.Services.UserManagement;

/**
 * Handles requests for the application home page.
 */
@Controller
@SessionAttributes("userLoggin")
public class HomeController {

	@Autowired
	private UserManagement userManagement;
	
	@Autowired
	private ProjectManagement projectManagement;

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView home(HttpSession session) {
		ModelAndView model = new ModelAndView();
		model.setViewName("home");
		session.removeAttribute("userLoggin");
		return model;
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView login(@RequestParam("username") String username,
			@RequestParam("password") String password) {
		ModelAndView model = new ModelAndView();
		User user = userManagement.checkLogin(username, password);
		if (user != null) {
			model.addObject("username", user);
			String groupCode = user.getGroup().getCode();
			boolean checkAvailable = userManagement.checkAvailable(username);
			if (groupCode.equals("PM")) {
				model.setViewName("listDEV");
				model.addObject("PMInfo", user);
				model.addObject("checkAvailable", checkAvailable);
				List<ProjectUser> projectUser = userManagement
						.findListProjectUserByUsername(user);
				model.addObject("project", projectUser);
				model.addObject("userLoggin", user);

				return model;
			} else if (groupCode.equals("DEV")) {

				List<ProjectUser> projectUser = userManagement
						.findListProjectUserByUsername(user);
				model.addObject("project", projectUser);
				model.setViewName("devInformation");
				model.addObject("DEVInfo", user);
				return model;
			} else {
				model.setViewName("listPM");
				List<User> users = userManagement.findAllUser();
				model.addObject("users", users);
				return model;
			}
		} else {
			model.setViewName("home");
			return model;
		}
	}

	@RequestMapping(value = "/remove/{username}/{PMname}/{projectCode}", method = RequestMethod.GET)
	public ModelAndView remove(@PathVariable("username") String username,
			@PathVariable("PMname") String PMname,
			@PathVariable("projectCode") String projectCode) {
		ModelAndView model = new ModelAndView();
		userManagement.removeDEV(username, projectCode);
		User user = userManagement.findUserByUserName(PMname);
		model.setViewName("listDEV");
		model.addObject("PMInfo", user);
		List<ProjectUser> projectUser = userManagement
				.findListProjectUserByUsername(user);
		model.addObject("project", projectUser);
		return model;
	}

	@RequestMapping(value = "/createPM", method = RequestMethod.GET)
	public ModelAndView createPM() {
		ModelAndView model = new ModelAndView();
		model.setViewName("createPM");
		return model;
	}

	@RequestMapping(value = "/createPM", method = RequestMethod.POST)
	public ModelAndView createPM(@RequestParam("username") String username,
			@RequestParam("password") String password,
			@RequestParam("fullname") String fullname,
			@RequestParam("address") String address,
			@RequestParam("phone") String phone,
			@RequestParam("selectCode") String code) {
		ModelAndView model = new ModelAndView();
		userManagement.createPM(username, password, fullname, address, phone,
				code);

		List<User> users = userManagement.findAllUser();
		model.addObject("users", users);
		model.setViewName("redirect:/listUser");
		return model;
	}

	@RequestMapping(value = "/listUser", method = RequestMethod.GET)
	public ModelAndView listUser() {
		ModelAndView model = new ModelAndView();
		List<User> users = userManagement.findAllUser();
		model.addObject("users", users);
		model.setViewName("listPM");
		return model;
	}

	@RequestMapping(value = "/update/{username}/{PMname}", method = RequestMethod.GET)
	public ModelAndView showUpdate(@PathVariable("username") String username,
			@PathVariable("PMname") String pmName,
			@ModelAttribute User userUpdate) {
		ModelAndView model = new ModelAndView();
		userUpdate = userManagement.findUserByUserName(username);
		model.addObject("userUpdate", userUpdate);
		model.addObject("PMInfo", pmName);
		model.setViewName("editInformation");
		return model;
	}

	@RequestMapping(value = "/update/{username}/{PMname}", method = RequestMethod.POST)
	public ModelAndView doUpdate(@PathVariable("username") String username,
			@PathVariable("PMname") String pmName,
			@ModelAttribute("fullNametxt") String fullname,
			@ModelAttribute("addresstxt") String address,
			@ModelAttribute("phonetxt") String phone) {
		ModelAndView model = new ModelAndView();
		User userOld = userManagement.findUserByUserName(username);
		userManagement.updateInformation(userOld,fullname, address, phone);
		model.setViewName("listDEV");
		User user = userManagement.findUserByUserName(pmName);
		model.addObject("PMInfo", user);

		List<ProjectUser> projectUser = userManagement
				.findListProjectUserByUsername(user);
		model.addObject("project", projectUser);

		return model;
	}

	@RequestMapping(value = "/updatePM/{username}", method = RequestMethod.GET)
	public ModelAndView showUpdatePM(@PathVariable("username") String username,
			@ModelAttribute User userUpdate) {

		ModelAndView model = new ModelAndView();
		userUpdate = userManagement.findUserByUserName(username);
		model.addObject("userUpdate", userUpdate);
		model.setViewName("editInformation");
		return model;

	}

	@RequestMapping(value = "/updatePM/{username}", method = RequestMethod.POST)
	public ModelAndView doUpdatePM(@PathVariable("username") String username,
			@ModelAttribute("fullNametxt") String fullname,
			@ModelAttribute("addresstxt") String address,
			@ModelAttribute("phonetxt") String phone) {
		ModelAndView model = new ModelAndView();
		User userOld = userManagement.findUserByUserName(username);
		userManagement.updateInformation(userOld,fullname, address, phone);
		model.setViewName("listDEV");
		User user = userManagement.findUserByUserName(username);
		model.addObject("PMInfo", user);

		List<ProjectUser> projectUser = userManagement
				.findListProjectUserByUsername(user);
		model.addObject("project", projectUser);

		return model;
	}

	@RequestMapping(value = "/changePasswordPM/{username}", method = RequestMethod.GET)
	public ModelAndView showChangePasswordPM(@PathVariable("username") String username,
			@ModelAttribute User userUpdate){

		ModelAndView model = new ModelAndView();
		userUpdate = userManagement.findUserByUserName(username);
		model.addObject("userUpdate", userUpdate);
		model.setViewName("changePassword");
		return model;

	}
	
	@RequestMapping(value = "/changePasswordPM/{username}", method = RequestMethod.POST)
	public ModelAndView dochangePasswordPM(@PathVariable("username") String username,
			@ModelAttribute("newPasswordtxt") String password) {
		ModelAndView model = new ModelAndView();
		User userOld = userManagement.findUserByUserName(username);
		userManagement.changePassword(userOld,password);
		model.setViewName("listDEV");
		User user = userManagement.findUserByUserName(username);
		model.addObject("PMInfo", user);
		
		List<ProjectUser> projectUser = userManagement.findListProjectUserByUsername(user);
		model.addObject("project", projectUser);
		
		return model;
	}
	
	@RequestMapping(value = "/updateDEV/{username}", method = RequestMethod.GET)
	public ModelAndView showUpdateDEV(
			@PathVariable("username") String username,
			@ModelAttribute User userUpdate) {

		ModelAndView model = new ModelAndView();
		userUpdate = userManagement.findUserByUserName(username);
		model.addObject("userUpdate", userUpdate);
		model.setViewName("editInformation");
		return model;

	}

	@RequestMapping(value = "/updateDEV/{username}", method = RequestMethod.POST)
	public ModelAndView doUpdateDEV(@PathVariable("username") String username,
			@ModelAttribute("fullNametxt") String fullname,
			@ModelAttribute("addresstxt") String address,
			@ModelAttribute("phonetxt") String phone) {
		ModelAndView model = new ModelAndView();
		User userOld = userManagement.findUserByUserName(username);
		userManagement.updateInformation(userOld,fullname, address, phone);;
		model.setViewName("devInformation");
		User user = userManagement.findUserByUserName(username);
		model.addObject("DEVInfo", user);

		List<ProjectUser> projectUser = userManagement
				.findListProjectUserByUsername(user);
		model.addObject("project", projectUser);

		return model;
	}
	
	@RequestMapping(value = "/changePasswordDEV/{username}", method = RequestMethod.GET)
	public ModelAndView showChangePasswordDEV(@PathVariable("username") String username,
			@ModelAttribute User userUpdate){

		ModelAndView model = new ModelAndView();
		userUpdate = userManagement.findUserByUserName(username);
		model.addObject("userUpdate", userUpdate);
		model.setViewName("changePassword");
		return model;

	}
	
	@RequestMapping(value = "/changePasswordDEV/{username}", method = RequestMethod.POST)
	public ModelAndView dochangePasswordDEV(@PathVariable("username") String username,
			@ModelAttribute("newPasswordtxt") String password) {
		ModelAndView model = new ModelAndView();
		User userOld = userManagement.findUserByUserName(username);
		userManagement.changePassword(userOld,password);
		model.setViewName("devInformation");
		User user = userManagement.findUserByUserName(username);
		model.addObject("DEVInfo", user);
		
		List<ProjectUser> projectUser = userManagement.findListProjectUserByUsername(user);
		model.addObject("project", projectUser);
		
		return model;
	}

	@RequestMapping(value = "/createDEV/{projectCode}/{PMName}", method = RequestMethod.GET)
	public ModelAndView createDEV(
			@PathVariable("projectCode") String projectCode,
			@PathVariable("PMName") String PMname) {
		ModelAndView model = new ModelAndView();
		model.addObject("projectCode", projectCode);
		model.setViewName("createDEV");
		return model;
	}

	@RequestMapping(value = "/createDEV/{projectCode}/{PMName}", method = RequestMethod.POST)
	public ModelAndView createDEV(@ModelAttribute("username") String username,
			@ModelAttribute("password") String password,
			@ModelAttribute("fullname") String fullname,
			@ModelAttribute("address") String address,
			@ModelAttribute("phone") String phone,
			@ModelAttribute("projectCode") String projectCode,
			@PathVariable("PMName") String PMname) {

		ModelAndView model = new ModelAndView();
		userManagement.createDEV(username, password, fullname, address, phone,
				projectCode);

		User pmUser = userManagement.findUserByUserName(PMname);
		model.setViewName("redirect:/listDEV/" + PMname);
		model.addObject("PMInfo", pmUser);
		List<ProjectUser> projectUser = userManagement
				.findListProjectUserByUsername(pmUser);
		model.addObject("project", projectUser);
		
		return model;
	}
	
	@RequestMapping(value = "/unprojectDEV/{PMname}", method = RequestMethod.GET)
	public ModelAndView listUnprojectDEV(@PathVariable("PMname") String pmName) {
		ModelAndView model = new ModelAndView();
		
		List<User> listUnprojectUsers = userManagement.findListUnprojectUser();
		model.addObject("listUnprojectUsers", listUnprojectUsers);
		User pmUser = userManagement.findUserByUserName(pmName);
		model.addObject("PMInfo", pmUser);
		model.setViewName("listUnprojectDEV");
		return model;
	}
	
	@RequestMapping(value = "/unprojectDEV/{PMname}/{DEVname}/{projectCode}", method = RequestMethod.GET)
	public ModelAndView assignDEVToProject(@PathVariable("PMname") String pmName,
				@PathVariable("DEVname") String devName,
				@PathVariable("projectCode") String projectCode) {
		ModelAndView model = new ModelAndView();		
				
		userManagement.assignDEVToProject(devName, projectCode);
		
		model.setViewName("redirect:/listDEV/" + pmName);
		return model;
	}
	
	@RequestMapping(value = "/listDEV/{PMname}", method = RequestMethod.GET)
	public ModelAndView listDEV(@PathVariable("PMname") String pmName) {
		ModelAndView model = new ModelAndView();
		User pmUser = userManagement.findUserByUserName(pmName);
		List<ProjectUser> projectUser = userManagement
				.findListProjectUserByUsername(pmUser);
		model.addObject("PMInfo", pmUser);
		model.addObject("project", projectUser);
		model.setViewName("listDEV");
		return model;
	}
	@RequestMapping(value = "/project/new/{PMname}", method = RequestMethod.GET)
	public ModelAndView createProject(@PathVariable("PMname") String PMname) {
		ModelAndView model = new ModelAndView();
		model.addObject("PMInfo", userManagement.findUserByUserName(PMname));
		model.setViewName("createProject");
		return model;
	}

	@RequestMapping(value = "/project/new/{PMname}", method = RequestMethod.POST)
	public ModelAndView createProject(@PathVariable("PMname") String PMname,
			@ModelAttribute("projectCodetxt") String code,
			@ModelAttribute("descriptiontxt") String description,
			@ModelAttribute("startday") String startday,
			@ModelAttribute("endday") String endday) {
		ModelAndView model = new ModelAndView();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date sday, eday;
		
		User user = userManagement.findUserByUserName(PMname);
		List<ProjectUser> projectUser = userManagement
				.findListProjectUserByUsername(user);
		
		try {
			sday = sdf.parse(startday);
			eday = sdf.parse(endday);
			projectManagement.createProject(PMname, code, description, sday, eday);
			
			model.addObject("project", projectUser);
			model.addObject("PMInfo", user);
			model.setViewName("listDEV");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException sql){
			sql.printStackTrace();
			model.addObject("errorMess", "Project is already existed!");
			model.addObject("PMInfo", user);
			model.setViewName("createProject");
		}
		
		return model;
	}

	/*@RequestMapping(value = "/createGroup", method = RequestMethod.POST)
	public ModelAndView createGroup(@RequestParam("groupCodetxt") String code,
			@RequestParam("descriptiontxt") String description) {
		ModelAndView model = new ModelAndView();	
		groupManagement.createGroup(code, description);
		model.setViewName("listPM");	
		return model;
	}*/
	
}
